classdef Measurement
  
    properties
        gen
        dc_sup 
        vna 
        psa 
       
    
    end
    
    methods
        
        function this = Measurement()
            %this.dc_sup = AG3648A('ni',0,8);
            this.vna = ZVA50('ni',0,20);
            %this.vna = PNA('ni',0,16);
            %this.psa = PXA('ni',0,18);
        end
        
        function delete(this)
            delete(this.dc_sup);
            delete(this.vna);
            delete(this.psa);
        end
        
         
        function [vgs,igs, vds, ids] = iv_char(this,vgs_start, vgs_stop,vgs_step, vds_start, vds_stop,vds_step,max_power,gs_chan,ds_chan)
         
          this.dc_sup.setOutput(1);
          i=1; 
          vvgs = vgs_start;
          while vvgs<vgs_stop
             
              
              %set vgs
              
              this.dc_sup.setVoltage(gs_chan,vvgs);
              
              vgs(i) = this.dc_sup.getVoltage(gs_chan);
              
              j=1;
              vvds = vds_start;
              
              while vvds< vds_stop
                  
                  
                  %set vds
                  this.dc_sup.setVoltage(ods_chan,vvds);
                  vds(i,j) = this.dc_sup.getVoltage(ds_chan);
                  %measure ids
                  ids(i,j) = this.dc_sup.getCurrent(ds_chan);
                  
                  
                  if (ids(i,j)*vds(j)>=max_power)
                      break;
                  end
                      
                   j=j+1;
                   vvds=vvds+vds_step;
                 
              end
              
              
          
              i=i+1;
              vvgs=vvgs+vgs_step;
              
          end
          
           this.dc_sup.setOutput(0);
              
        end
        
        
        function [freqs,s11,s12,s21,s22] = freqSweep(this, startFreq,stopFreq,power,sweepPoints,bandwidth)

            this.vna.deleteAllTraces();
            this.vna.addTrace(1,'S11','S11');
            this.vna.addTrace(2,'S12','S12');
            this.vna.addTrace(3,'S22','S22');
            this.vna.addTrace(4,'S21','S21');
            
            this.vna.sweepType = 'LIN';
            
            this.vna.sweepPoints = sweepPoints; %
            
           
            %this.vna.sweepCount = 1;
       
            this.vna.startFrequency = startFreq;
           
            this.vna.stopFrequency = stopFreq;
          
            this.vna.power = power;
           
            this.vna.bandwidth = bandwidth;
           
            %this.vna.setLowPhaseNoise(1,'on');
            %select s-params
            %this.vna.selectSParameters(1,'S11','S11');
            %this.vna.selectSParameters(1,'S12','S12');
            %this.vna.selectSParameters(1,'S22','S22');
            %this.vna.selectSParameters(1,'S21','S21');
          
            this.vna.setTraceFormat('MLOG');
            this.vna.setDataFormat('REAL,32');
            %set single sweep
            this.vna.setSingleSweep('off');
           
            this.vna.trigger();

            this.vna.setAutoScale();
           
            
            
            %%r&s zva50
%             response = this.vna.getResponseData();
%             freqs = this.vna.getStimulusData();
%             s11 = this.vna.parseParameter(1,sweepPoints,response,1);
%             s12 = this.vna.parseParameter(1+2*sweepPoints,sweepPoints,response,1);
%             s22 = this.vna.parseParameter(1+4*sweepPoints,sweepPoints,response,1);
%             s21 = this.vna.parseParameter(1+6*sweepPoints,sweepPoints,response,1);
            
            %%%agilent pna-x
            freqs =5;
            s11 = this.vna.getResponseData('S11');
            s12 = this.vna.getResponseData('S12');
            s22 = this.vna.getResponseData('S22');
            s21 = this.vna.getResponseData('S21');
            
           
            %this.vna.closeWindow();

            
            
        end
        
        function [pin,pout,s21] = powerSweep(this,freq,startPower,stopPower,sweepPoints,bandwidth)
            
            
            this.vna.deleteAllTraces();
            
            %add trace 
            if strcmp(class(this.vna),'PNA') == 1 
                this.vna.addTrace(1,'B2','B2,2');
            elseif strcmp(class(this.vna),'ZVA50') == 1
                this.vna.addTrace(1,'B2','B2');
            end
            this.vna.addTrace(2,'S21','S21');
            %add more parameters here
          
            
            
           this.vna.sweepType = 'POWER';
            
            this.vna.sweepPoints = sweepPoints; %
           
            
            
            
            %set power sweep
           
           
            this.vna.frequency = freq;
            
            this.vna.startPower = startPower;
           
            this.vna.stopPower = stopPower;
            %set bandwidth
            this.vna.bandwidth = bandwidth;
            
            %set low phase noise
            %this.vna.setLowPhaseNoise(1,'on');
                      
            
           
            this.vna.setTraceFormat('MLOG');
            this.vna.setDataFormat('REAL,32');
            
            this.vna.setSingleSweep('off');
            
            this.vna.trigger();           
           
            this.vna.setAutoScale();
            
            
            pin = 5;
            pout = this.vna.getResponseData('B2');
            s21 = this.vna.getResponseData('S21');
            
            
            
           
            %this.vna.closeWindow();
            
            %data processing
            
            %add more parameters here 3
            
        end
        
        function [freq_range,data] = getSpectrum(this,startFreq,stopFreq, rbw, sweepPoints)
            
            this.psa.setStartFreq(startFreq);
            this.psa.setStopFreq(stopFreq);
            this.psa.setRBW(rbw);
            this.psa.setSweepPoints(sweepPoints);
            
            fstep = (stopFreq-startFreq)/sweepPoints;
            %calculate freqs list
            for t=1:sweepPoints
                if t == 1 
                    freq_range(t) = startFreq;
                else
                    freq_range(t) = startFreq + t * fstep;
                end
            end
            
            data = this.psa.getTraceData();
        
        end
        
        
        
            
            
    end
    
end